<?php
namespace App\Admin\Controllers;
use App\Admin\Actions\Post\AutoMsgVerfyBad;
use App\Admin\Actions\Post\AutoMsgVerfyOk;
use App\Admin\Extensions\AutoMsgOp;
use App\Model\AppChannel;
use App\Model\AppVersion;
use App\Model\SettingAppHidden;
use App\Model\UserAutoMsg;
use App\Util\ComUtil;
use Encore\Admin\Controllers\AuthController as BaseAuthController;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Model\AppStandard;

class AppStandardController extends BaseAuthController
{
    use ModelForm;

    function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('协议和隐私政策管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        $grid = new Grid(new AppStandard());
        $where = [];
        $grid->model()->where($where)->orderBy('id', 'desc');
        $grid->id('记录ID');
        $grid->channel('应用')->display(function ($data){
           return AppStandard::getChannel($this->channel);
        });

        $grid->check_channel('渠道')->display(function ($data) {
            return $data ? $data : '';
        });
        $grid->type('类型')->display(function ($data){
            return AppStandard::getType($data);
        });
        $grid->link_url('链接');
        $grid->flag('状态')->display(function ($data){
            return $data == 1 ? '生效' : '失效';
        });
        $grid->created_at('设置时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableFilter();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            // $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('创建');
        $content->body($this->form());
        return $content;
    }

    protected function form()
    {
        $form = new Form(new AppStandard());
        $form->select('channel', '应用')->options(AppStandard::CHANNEL)->default(100)->required();
        $form->multipleSelect('check_channel', '渠道')->options(AppChannel::getChannelsAll())->required();
        $form->select('type', '类型')->options(AppStandard::TYPE)->default(1)->required();
        $form->text('link_url','链接');
        $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
        return $form;
    }
}
